#!/bin/bash

sdk_path="../.."

toolspath=./toolspath.txt
tools_type="dev"
ts_path=ts-swift-workload
code_ts_lib="BenchmarkMeasure"
target_path=""

if [ -s "$toolspath" ]; then
    while IFS= read -r line; do
        case $line in
            *--ts-tools-path\ *)
                sdk_path="${line#*--ts-tools-path\ }"
                ;;
            *--ark_js_vm\ *)
                ark_js_vm="${line#*--ark_js_vm\ }"
                ;;
            *--es2abc\ *)
                es2abc="${line#*--es2abc\ }"
                ;;
            *--ark_aot_compiler\ *)
                ark_aot_compiler="${line#*--ark_aot_compiler\ }"
                ;;
            *--tools-type\ *)
                tools_type="${line#*--tools-type\ }"
                ;;
            *--case-path\ *)
                ts_path="${line#*--case-path\ }"
                ;;
        esac
    done < "toolspath.txt"
fi

# 将带“~”的路径转换成绝对路径，保证脚本获取路径的准确性，部分命令不能使用“~”
evaluated_path=$(eval "echo $sdk_path")
sdk_path=$(realpath "$evaluated_path")

arg_name=""
true="true"
false="false"
run_count=1
aarch64=""
date=$(date +'%Y%m%d%H%M%S')

function help_explain(){
    echo "[--help,--h]"
    echo "[--run-count <count>]"
    echo "[--case <case-name>]"
    echo "[--date <date>]"
    echo "[--build]"
    echo "[--excel]"
    echo "[--aarch64]"
}
if [ -z "$1" ]; then
    echo "No input provided."
else
    for arg in "$@"; do
        case $arg_name in
            "--case")
                arg_name=""
                case_name=$arg
                ;;
            "--date")
                arg_name=""
                date=$arg
                ;;
            "--run-count")
                arg_name=""
                run_count=$arg
                ;;
        esac
        case $arg in
            "--h")
                help_explain
                exit
                ;;
            "--help")
                help_explain
                exit
                ;;
            "--build")
                build="true"
                ;;
            "--case")
                arg_name=$arg
                ;;
            "--date")
                arg_name=$arg
                ;;
            "--excel")
                excel="true"
                ;;
            "--run-count")
                arg_name=$arg
                ;;
            "--aarch64")
                aarch64="--compiler-target-triple=aarch64-unknown-linux-gnu "
                ;;
        esac
    done
fi
case $tools_type in
    "dev")
        eval export LD_LIBRARY_PATH=${sdk_path}/out/x64.release/arkcompiler/ets_runtime:${sdk_path}/out/x64.release/thirdparty/icu:${sdk_path}/prebuilts/clang/ohos/linux-x86_64/llvm/lib:${sdk_path}/out/x64.release/thirdparty/zlib
        target_path=x64.release
        ;;
    "rk3568")
        echo "eval export LD_LIBRARY_PATH=${sdk_path}/out/rk3568/clang_x64/arkcompiler/ets_runtime:${sdk_path}/out/rk3568/clang_x64/thirdparty/icu:${sdk_path}/out/rk3568/clang_x64/thirdparty/zlib:${sdk_path}/out/rk3568/clang_x64/thirdparty/cjson:${sdk_path}/prebuilts/clang/ohos/linux-x86_64/llvm/lib" > compile_env.log
        eval export LD_LIBRARY_PATH=${sdk_path}/out/rk3568/clang_x64/arkcompiler/ets_runtime:${sdk_path}/out/rk3568/clang_x64/thirdparty/icu:${sdk_path}/out/rk3568/clang_x64/thirdparty/zlib:${sdk_path}/out/rk3568/clang_x64/thirdparty/cjson:${sdk_path}/prebuilts/clang/ohos/linux-x86_64/llvm/lib
        target_path=rk3568/clang_x64
        ;;
    "hispark_taurus")
        eval export LD_LIBRARY_PATH=${sdk_path}/out/hispark_taurus/clang_x64/arkcompiler/ets_runtime:${sdk_path}/out/hispark_taurus/clang_x64/thirdparty/icu:${sdk_path}/out/hispark_taurus/clang_x64/thirdparty/zlib:${sdk_path}/prebuilts/clang/ohos/linux-x86_64/llvm/lib
        target_path=hispark_taurus/clang_x64
        ;;
esac

es2abc=${sdk_path}/out/${target_path}/arkcompiler/ets_frontend/es2abc
ark_aot_compiler=${sdk_path}/out/${target_path}/arkcompiler/ets_runtime/ark_aot_compiler
ark_js_vm=${sdk_path}/out/${target_path}/arkcompiler/ets_runtime/ark_js_vm

eval chmod a+x ${es2abc}
eval chmod a+x ${ark_aot_compiler}
eval chmod a+x ${ark_js_vm}

pgo2path="pgo_build/${date}"
eval $es2abc $sdk_path/arkcompiler/ets_runtime/ecmascript/ts_types/lib_ark_builtins.d.ts --type-extractor --type-dts-builtin --module --merge-abc --extension=ts --output $sdk_path/arkcompiler/ets_runtime/ecmascript/ts_types/lib_ark_builtins.d.abc
function build_pgo(){
    local ts_file=$1
    local file_name=$2
    local abc_file=${pgo2path}/${file_name}.abc
    echo "build : ${ts_file} to ${pgo2path}"
    eval ${es2abc} ${ts_file} --type-extractor --module --merge-abc --extension=ts --output ${pgo2path}/${file_name}.abc
    
    if [ "${code_ts_lib}" == "${file_name}" ];then
        echo "code_ts_lib: ${code_ts_lib}   :    file_name:${file_name}"
        return 1
    fi
    eval ${ark_js_vm} --log-level=info --enable-pgo-profiler=true --compiler-opt-inlining=true --compiler-pgo-profiler-path=./${pgo2path}/${file_name}.ap --log-level=info --asm-interpreter=true --entry-point=${file_name} ${abc_file}
    echo "eval time timeout 300s ${ark_aot_compiler} --log-level=info --compiler-log-time=true --compiler-opt-loop-peeling=true --compiler-fast-compile=false --compiler-opt-track-field=true --compiler-opt-inlining=true --compiler-max-inline-bytecodes=45 --compiler-opt-level=0 --builtins-dts=${sdk_path}/arkcompiler/ets_runtime/ecmascript/ts_types/lib_ark_builtins.d.abc --compiler-pgo-profiler-path=./${pgo2path}/${file_name}.ap ${aarch64} --aot-file=./${pgo2path}/${file_name} ${abc_file}" >> ./${pgo2path}/time.log
    for i in $(seq 1 5)
    do
    	eval time timeout 300s  ${ark_aot_compiler} --log-level=info --compiler-log-time=true --compiler-opt-loop-peeling=true --compiler-fast-compile=false --compiler-opt-track-field=true --compiler-opt-inlining=true --compiler-max-inline-bytecodes=45 --compiler-opt-level=0 --builtins-dts=${sdk_path}/arkcompiler/ets_runtime/ecmascript/ts_types/lib_ark_builtins.d.abc --compiler-pgo-profiler-path=./${pgo2path}/${file_name}.ap ${aarch64} --aot-file=./${pgo2path}/${file_name} ${abc_file} >> ./${pgo2path}/time.log 2>&1
    done
}

function run_pgo(){
    local file_name=$1
    local abc_file=${pgo2path}/${file_name}.abc
    echo run ${file_name}
    eval ${ark_js_vm} --entry-point=${file_name} --aot-file=./${pgo2path}/${file_name} ${abc_file}
}

function run(){
    if [[ "$build" = "true" ]]; then
        if [ ! -d $pgo2path ];then
            mkdir -p $pgo2path
        fi
        code_ts_lib_file=$(find $ts_path -type f -name ${code_ts_lib}.ts)
        if [ ${code_ts_lib_file} != "" ]; then
            file_name=$(basename "${code_ts_lib_file}" | cut -f1 -d'.')
            eval ${es2abc} ${code_ts_lib_file} --type-extractor --module --merge-abc --extension=ts --output pgo_build/${file_name}.abc
        fi

        if [ -z "$case_name" ]; then
            txt_files=($(find $ts_path -type f -name "*.ts"))
            for file in "${txt_files[@]}"; do
                echo "Content of $file:"
                local ts_file_path=$file
                local file_name=$(basename "${file}" | cut -f1 -d'.')
                build_pgo ${ts_file_path} ${file_name}
                if [[ "$excel" = "true" ]]; then
                    if [ "${code_ts_lib}" == "${file_name}" ];then
                        echo "code_ts_lib: ${code_ts_lib}   :    file_name:${file_name}"
                        continue
                    fi
                    python pgo2excel.py --case-dir $pgo2path --run-case $file_name --run-count $run_count --ark_js_vm $ark_js_vm
                fi
                done
        else
            local ts_file_path=$(find ${ts_path} -name "${case_name}.ts")
            local file_name=$(basename "$ts_file_path" | cut -f1 -d'.')
            if [ -f "${ts_file_path}" ]; then
                build_pgo ${ts_file_path} ${file_name}
                if [[ "$excel" = "true" ]]; then
                    if [ "${code_ts_lib}" == "${file_name}" ];then
                        echo "code_ts_lib: ${code_ts_lib}   :    file_name:${file_name}"
                        continue
                    fi
                    python pgo2excel.py --case-dir $pgo2path --run-case $file_name --run-count $run_count --ark_js_vm $ark_js_vm
                fi
            fi
        fi
    fi

    if [ ! -d $pgo2path ];then
        echo "${pgo2path} No such file or directory"
        exit
    fi
}
run

